package com.example.administrator.zxinglibrary;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.seppdzxing.SeppdZxingView;
import com.example.administrator.seppdzxing.activity.ResultActivity;
import com.example.administrator.seppdzxing.decode.DecodeThread;
import com.example.administrator.seppdzxing.listener.ScanResultLisinter;
import com.google.zxing.Result;

public class MainActivity extends AppCompatActivity implements ScanResultLisinter {

    private SeppdZxingView zxingView;
    private static int REQUEST_CODE_CHOOSE = 10;
    private ImageView imageView;
    private AlertDialog alertDialog;
    private TextView content;
    private long statrTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        zxingView = findViewById(R.id.zixing);
        imageView = findViewById(R.id.src);
        zxingView.setScanListener(this);

//        findViewById(R.id.open).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
//                photoPickerIntent.setType("image/*");
//                startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE);
//            }
//        });

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        alertDialog = builder
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        zxingView.restartSport();
                        statrTime = System.currentTimeMillis();
                    }
                })
                .create();

    }

    @Override
    protected void onResume() {
        super.onResume();
        zxingView.onResume(false);
    }


    @Override
    protected void onStop() {
        super.onStop();
        zxingView.onStop();
    }


    @Override
    public void handleDecode(Result rawResult, Bundle bundle) {
        long l = System.currentTimeMillis() - statrTime;
        Log.d("AAA","间隔"+l);
        alertDialog.setTitle(rawResult.getText());
        alertDialog.show();
//        Log.d("AAA","result "+rawResult.getText());
//        bundle.putInt("width",  zxingView.getCropRect().width());
//        bundle.putInt("height", zxingView.getCropRect().height());
//        bundle.putString("result", rawResult.getText());
////
//        startActivity(new Intent(this, ResultActivity.class).putExtras(bundle));
        byte[] compressedBitmap = bundle.getByteArray(DecodeThread.BARCODE_BITMAP);
        if (compressedBitmap != null) {
            Bitmap barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);
            // Mutable copy:
            barcode = barcode.copy(Bitmap.Config.RGB_565, true);
            imageView.setImageBitmap(barcode);
        }

//        zxingView.restartSport();

    }

    @Override
    public void handleDecodeBitmapError() {
        Log.d("AAA","失败");
    }

    @Override
    public void openCameraError() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE) {
            handleImage(data);
        }
    }
    private void handleImage(final Intent data) {
        String bitmap = getRealPathFromURI(data.getData());
        //解析二维码
        if(!TextUtils.isEmpty(bitmap)){
            zxingView.decodeBitmap(bitmap);
        }
    }
    public  String getRealPathFromURI( Uri contentURI) {
        String result;
        Cursor cursor = getContentResolver().query(contentURI,
                new String[]{MediaStore.Images.ImageColumns.DATA},//
                null, null, null);
        if (cursor == null) {
            result = contentURI.getPath();
        }else {
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(index);
            cursor.close();
        }
        return result;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
